package cn.itcast.server.service.handler;

import cn.itcast.message.ChatRequestMessage;
import cn.itcast.message.ChatResponseMessage;
import cn.itcast.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        //根据客户端发来的信息获取到私聊对象信息
        String man = msg.getTo();
        Channel channel = SessionFactory.getSession().getChannel(man);
        //将信息发送给私聊对象
        if(channel != null){
            channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(), msg.getContent()));
            ctx.writeAndFlush(new ChatResponseMessage(true, "信息发送成功！"));
        }else {
            ctx.writeAndFlush(new ChatResponseMessage(false, "当前用户不在线！"));
        }
    }
}
